Skip to content

feat(browser): export aria tree utils#10171

Open
hi-ogawa wants to merge 12 commits intovitest-dev:mainfrom
hi-ogawa:feat-export-aria-tree-utils
Open

feat(browser): export aria tree utils#10171
hi-ogawa wants to merge 12 commits intovitest-dev:mainfrom
hi-ogawa:feat-export-aria-tree-utils

Conversation

@hi-ogawa
Copy link
Copy Markdown
Collaborator

@hi-ogawa hi-ogawa commented Apr 22, 2026

Description

This PR exposes ivya/aria exports via import { utils } from "vitest/browser". This can be useful as a standalone primitive of "node to aria tree" serialization similar to prettyDOM does for "ndoe to html like" serialization.

import { utils } from 'vitest/browser'

document.body.innerHTML = `
  <h1>Hello, World!</h1>
  <button aria-hidden="true">Hidden</button>
  <button>Visible</button>
`
const tree = utils.aria.generateAriaTree(document.body)
const yaml = utils.aria.renderAriaNode(tree)
console.log(yaml)
// - heading "Hello, World!" [level=1]
// - button "Visible""

The wiring is a bit mess like __INTERNAL and @vitest/browser/internal/vendor-types, but seems necessary to avoid duplicating modules and also hand-rolling same type definition. For example, I noticed type AriaRole is currently manually written in packages/browser/aria-role.d.ts but this can also come from ivya with similar tricks.

Please don't delete this checklist! Before submitting the PR, please make sure you do the following:

  • It's really useful if your PR references an issue where it is discussed ahead of time. If the feature is substantial or introduces breaking changes without a discussion, PR might be closed.
  • Ideally, include a test that fails without this PR but passes with it.
  • Please, don't make changes to pnpm-lock.yaml unless you introduce a new test example.
  • Please check Allow edits by maintainers to make review process faster. Note that this option is not available for repositories that are owned by Github organizations.

Tests

  • Run the tests with pnpm test:ci.

Documentation

  • If you introduce new functionality, document it. You can run documentation with pnpm run docs command.

Changesets

  • Changes in changelog are generated from PR name. Please, make sure that it explains your changes in an understandable manner. Please, prefix changeset messages with feat:, fix:, perf:, docs:, or chore:.

@netlify
Copy link
Copy Markdown

netlify Bot commented Apr 22, 2026

Deploy Preview for vitest-dev ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit a69091c
🔍 Latest deploy log https://app.netlify.com/projects/vitest-dev/deploys/69e8635bf7b2780008818572
😎 Deploy Preview https://deploy-preview-10171--vitest-dev.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new Bot commented Apr 22, 2026

@vitest/browser

npm i https://pkg.pr.new/@vitest/browser@10171

@vitest/browser-playwright

npm i https://pkg.pr.new/@vitest/browser-playwright@10171

@vitest/browser-preview

npm i https://pkg.pr.new/@vitest/browser-preview@10171

@vitest/browser-webdriverio

npm i https://pkg.pr.new/@vitest/browser-webdriverio@10171

@vitest/coverage-istanbul

npm i https://pkg.pr.new/@vitest/coverage-istanbul@10171

@vitest/coverage-v8

npm i https://pkg.pr.new/@vitest/coverage-v8@10171

@vitest/expect

npm i https://pkg.pr.new/@vitest/expect@10171

@vitest/mocker

npm i https://pkg.pr.new/@vitest/mocker@10171

@vitest/pretty-format

npm i https://pkg.pr.new/@vitest/pretty-format@10171

@vitest/runner

npm i https://pkg.pr.new/@vitest/runner@10171

@vitest/snapshot

npm i https://pkg.pr.new/@vitest/snapshot@10171

@vitest/spy

npm i https://pkg.pr.new/@vitest/spy@10171

@vitest/ui

npm i https://pkg.pr.new/@vitest/ui@10171

@vitest/utils

npm i https://pkg.pr.new/@vitest/utils@10171

vitest

npm i https://pkg.pr.new/vitest@10171

@vitest/web-worker

npm i https://pkg.pr.new/@vitest/web-worker@10171

@vitest/ws-client

npm i https://pkg.pr.new/@vitest/ws-client@10171

commit: 633fdbd

Comment on lines +939 to +954
/**
* Utilities for generating and working with ARIA trees and templates.
* @experimental
*/
aria: {
/** Captures the ARIA tree for a DOM subtree. */
generateAriaTree: typeof __ivyaAriaTypes.generateAriaTree
/** Renders a captured ARIA tree to the textual snapshot format. */
renderAriaTree: typeof __ivyaAriaTypes.renderAriaTree
/** Renders an ARIA template back to text. */
renderAriaTemplate: typeof __ivyaAriaTypes.renderAriaTemplate
/** Parses textual ARIA snapshot syntax into a template tree. */
parseAriaTemplate: typeof __ivyaAriaTypes.parseAriaTemplate
/** Matches a captured ARIA tree against a parsed template. */
matchAriaTree: typeof __ivyaAriaTypes.matchAriaTree
}
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typescript couldn't infer jsdoc from the original ivya/aria exports (likely due to dts bundling), so I wrote up it manually by partially duplicating the ones in ivya.

@hi-ogawa hi-ogawa requested a review from AriPerkkio April 22, 2026 06:40
@hi-ogawa hi-ogawa marked this pull request as ready for review April 22, 2026 06:40
AriPerkkio
AriPerkkio previously approved these changes Apr 22, 2026
Copy link
Copy Markdown
Member

@AriPerkkio AriPerkkio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, this will allow users to use a11y tree utils without having to install another ivya next to the bundled one.

Comment on lines +559 to +561
get aria() {
return __INTERNAL._aria
},
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What exactly does this work-around fix? Why not import { aria } from "./aria" and return that here?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's some weird @vitest/browser client bundle split and we have tester/context.ts and tester/expect-element.ts independently built without sharing chunks. Previously ivya/aria lived only in expect-element bundle, but this one now needs to be available from context.ts, so it's passed through __INTERNAL global.

There's other globals that are passed around like __vitest_browser_runner__, but haven't fully understood what is for what.

@JReinhold
Copy link
Copy Markdown

This is pretty awesome! How does it compare to Playwright's mode: 'ai' | 'default' option? https://playwright.dev/docs/api/class-page#page-aria-snapshot

Where the AI mode also captures iframe's contents and adds unique references to each element.

@hi-ogawa
Copy link
Copy Markdown
Collaborator Author

This is pretty awesome! How does it compare to Playwright's mode: 'ai' | 'default' option? https://playwright.dev/docs/api/class-page#page-aria-snapshot

Where the AI mode also captures iframe's contents and adds unique references to each element.

Our implementation is stripped down port and AI part isn't included. https://github.com/vitest-dev/ivya/blob/7d84867d3576ac9c7342299e552d3c78a60a272b/src/aria/folk/injected/ariaSnapshot.ts#L17-L20

It's expected to exactly match with their default mode though haven't verified. I haven't looked into beyond what's needed for Vitest's toMatchAriaSnapshot. If it's useful as standalone or relevant to snapshot feature, we can probably try supporting that too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants